package com.woniuxy.joylifeshop.controller;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.ShopAddress;
import com.woniuxy.common.entity.ShopUserAddress;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.AddressException;
import com.woniuxy.joylifeshop.service.AddressService;
import com.woniuxy.joylifeshop.service.CenterUserService;
import com.woniuxy.joylifeshop.service.ShopUserAddressService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class AddressController {

    @Autowired
    AddressService addressService;

    @Autowired
    CenterUserService centerUserService;

    @Autowired
    ShopUserAddressService shopUserAddressService;

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @RequestMapping("/deleteAddress")
    public ResponseInformation deleteAddress(String addrId){
        ResponseInformation responseInformation = new ResponseInformation();


        try {
            shopUserAddressService.deleteAddressAndUserAddress(addrId);

            responseInformation.setMsg("删除成功");
            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setMsg("删除失败");
            return responseInformation;
        }
    }

    /**
    * @Author SooYoo
    * @Description 编辑地址
    * @Param
    * @Return
    **/
    @RequestMapping("/editAddress")
    public ResponseInformation editAddress (String addrId,String name,String phone,String
      country,String province,String city,String detail,boolean priority,HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        System.out.println(addrId+name+phone+country+province+city+detail+priority);
        ResponseInformation responseInformation = new ResponseInformation();


            if (name.equals("") || name==null){
                responseInformation.setMsg("姓名不能为空");
                return responseInformation;
            }
            if (phone.equals("") || phone==null){
                responseInformation.setMsg("电话不能为空");
                return responseInformation;
            }
            if (country.equals("") || country==null){
                responseInformation.setMsg("国家不能为空");
                return responseInformation;
            }
            if (province.equals("") || province==null){
                responseInformation.setMsg("省份不能为空");
                return responseInformation;
            }
            if (city.equals("") || city==null){
                responseInformation.setMsg("城市不能为空");
                return responseInformation;
            }
            if (detail.equals("") || detail==null){
                responseInformation.setMsg("详细地址不能为空");
                return responseInformation;
            }

        try {
            //修改userAddress对象
            addressService.modifyAddress(addrId,name,phone,country,province,city,detail,priority,userId);
            responseInformation.setMsg("修改成功");

            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setMsg("fail");
            return responseInformation;
        }
    }

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @RequestMapping("/initEditAddress")
    public ResponseInformation initEditAddress(String addrId){
        ResponseInformation responseInformation = new ResponseInformation();

        ShopUserAddress shopUserAddress = addressService.readOneUserAddress(addrId);

        ShopAddress shopAddress = addressService.readOneAddress(addrId);

        responseInformation.setData(shopUserAddress);
        responseInformation.setData2(shopAddress);
        responseInformation.setMsg("success");
        return responseInformation;
    }


    /**
    * @Author SooYoo
    * @Description 新增一个地址
    * @Param
    * @Return
    **/
    @RequestMapping("/addAddress")
    public ResponseInformation addAddress(String name,String phone,String country,String province,
    String city,String detail, HttpServletRequest request){
        ResponseInformation responseInformation = new ResponseInformation();
        String userId = JWTUtils.getUserId(request.getHeader("token"));

        try {
            if (name.equals("") || name==null){
                responseInformation.setMsg("姓名不能为空");
                return responseInformation;
            }
            if (phone.equals("") || phone==null){
                responseInformation.setMsg("电话不能为空");
                return responseInformation;
            }
            if (country.equals("") || country==null){
                responseInformation.setMsg("国家不能为空");
                return responseInformation;
            }
            if (province.equals("") || province==null){
                responseInformation.setMsg("省份不能为空");
                return responseInformation;
            }
            if (city.equals("") || city==null){
                responseInformation.setMsg("城市不能为空");
                return responseInformation;
            }
            if (detail.equals("") || detail==null){
                responseInformation.setMsg("详细地址不能为空");
                return responseInformation;
            }
            //新建一个地址对象
            ShopAddress shopAddress = shopUserAddressService.addShopAddress(country, province, city, detail);
            String addrId = shopAddress.getAddrId();
            //新建一个地址映射对象
            ShopUserAddress shopUserAddress = shopUserAddressService.addShopUserAddress(name, phone, userId,addrId);

            responseInformation.setState(200);
            responseInformation.setMsg("新建地址成功");
            return  responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setMsg("未知错误");

            return responseInformation;
        }
    }

    /**
    * @Author SooYoo
    * @Description  找到非默认的映射地址,和非默认的地址实体
    * @Param
    * @Return
    **/
    @RequestMapping("/initOtherAddress")
    public ResponseInformation initOtherAddress (String addrId, HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));


        ResponseInformation responseInformation = new ResponseInformation();

        try {
            ObjectDto objectDto = addressService.readOtherMapAddressAndAddressObject(addrId, userId);

            responseInformation.setMsg("更换地址成功");
            responseInformation.setState(200);
            responseInformation.setData(objectDto);

            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setState(500);
            responseInformation.setMsg("更换地址失败");

            return responseInformation;
        }

    }


    /**
    * @Author SooYoo
    * @Description 初始化订单确认页面的默认地址
    * @Param
    * @Return
    **/
    @RequestMapping("/initDefaultAddress")
    public ResponseInformation initDefaultAddress(HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));

        ResponseInformation responseInformation = new ResponseInformation();

        try {
            ObjectDto objectDto = addressService.readDefaultAddress(userId);

            responseInformation.setMsg("找到默认地址成功");
            responseInformation.setState(200);
            responseInformation.setData(objectDto);

            return responseInformation;
        } catch (AddressException e) {
            e.printStackTrace();

            responseInformation.setMsg("找到默认地址失败");
            responseInformation.setState(500);

            return responseInformation;
        }
    }


    /**
    * @Author SooYoo
    * @Description 初始化地址
    * @Param
    * @Return
    **/
    @RequestMapping("/initAddress")
    public ResponseInformation initAddress (HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));

        //System.out.println(userId);
        ResponseInformation responseInformation = new ResponseInformation();

        try {
            //找到所有的映射对象
            List<ShopUserAddress> shopUserAddresses = addressService.readUserMapObject(userId);
            //找到所有实际地址对象
            List<ShopAddress> shopAddresses = addressService.readUserRealityAddressObject(userId);
            //找到用户对象
            CenterUser centerUser = centerUserService.readUserByUserId(userId);

            responseInformation.setData(shopUserAddresses);
            responseInformation.setData2(shopAddresses);
            responseInformation.setData3(centerUser);
            responseInformation.setMsg("寻找地址成功");
            responseInformation.setState(200);

            return responseInformation;
        } catch (AddressException e) {
            e.printStackTrace();

            responseInformation.setMsg("寻找地址失败");
            responseInformation.setState(500);

            return responseInformation;
        }
    }





}
